package com.wwwarehouse.usercenter.fragment.register;

import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.custom_widget.popupwindow.EasyPopupWindow;
import com.wwwarehouse.common.eventbus_event.CardDeskEvent;
import com.wwwarehouse.common.tools.PopUpUtils;
import com.wwwarehouse.usercenter.R;

import de.greenrobot.event.EventBus;


/**
 * @author : mundane
 * @time : 2017/9/28 19:01
 * @description :
 * @file : AlreadySubmitFragment.java
 */

public class AlreadySubmitFragment extends BaseFragment {

	private View            mBtnClose;
	private View            mLlCall;
	private EasyPopupWindow mEasyPopupWindow;
	private static final String KEY_INVITE_MOBILE = "key_invite_mobile";
	private String mInviteMobile;

	public static AlreadySubmitFragment newInstance(String inviteMobile) {
		AlreadySubmitFragment fragment = new AlreadySubmitFragment();
		Bundle bundle = new Bundle();
		bundle.putString(KEY_INVITE_MOBILE, inviteMobile);
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle bundle = getArguments();
		if (bundle != null) {
			mInviteMobile = bundle.getString(KEY_INVITE_MOBILE);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_submit_already, container, false);
		return view;
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		// findViewById
		mBtnClose = $(R.id.btn_close);
		mLlCall = $(R.id.ll_call);
		mBtnClose.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
//				popFragment();
				EventBus.getDefault().post(new CardDeskEvent("clear_back_stack"));
			}
		});
		mLlCall.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mEasyPopupWindow = PopUpUtils.showPopUpBottom(R.layout.layout_contact_person, getContext(), mActivity.findViewById(android.R.id.content), true, childClickListener);

			}
		});
	}

	EasyPopupWindow.ChildClickListener childClickListener = new EasyPopupWindow.ChildClickListener() {
		@Override
		public void getChildView(int layoutId, View view) {
			view.findViewById(R.id.btn_wechat_contact).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					try {
						Intent intent = new Intent(Intent.ACTION_MAIN);
						ComponentName cmp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");
						intent.addCategory(Intent.CATEGORY_LAUNCHER);
						intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						intent.setComponent(cmp);
						startActivity(intent);
					} catch (Exception e) {
						Toast.makeText(getContext(), "检查到您手机没有安装微信，请安装后使用该功能", Toast.LENGTH_LONG).show();
					}
				}
			});

			view.findViewById(R.id.btn_phone_contact).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + mInviteMobile));
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					startActivity(intent);
				}
			});

			view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					mEasyPopupWindow.dismiss();
				}
			});
		}
	};


	@Override
	public void setTitle() {
		if (peekFragment() instanceof AlreadySubmitFragment) {
			mActivity.setTitle("修改注册信息");
		}
	}

	@Override
	public void requestDatas() {

	}
}
